package com.zhai.test.algorithm.array;

/**
 * @Description: 二分查找计算x 的平方根
 * @ClassName: CacaulateSqureValue
 * @Author: zhaiyongxin
 * @Date: 2025/3/12 19:37
 * @Version: 1.0
 */
public class CalculateSquareValue {
    public static void main(String[] args) {
        int x = 4;
        int i = void1(1);
        System.out.println(i);

    }

    public static int void1(int x) {
        if (x == 0 || x == 1){
            return x;
        }
        int left = 0;
        int right = x;
        while(left <= right) {
            int mid = left + (right - left) / 2;
            if (mid == x/mid) {
                return mid;
            } else if (mid < x/mid) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return right;
    }

}
